package com.rgs.module.login.service.impl;

import cn.hutool.core.util.IdUtil;
import com.google.common.base.Strings;
import com.rgs.base.database.SqlPlusUtil;
import com.rgs.module.login.entity.SysUserDO;
import com.rgs.module.login.filter.SysUserFilterDTO;
import com.rgs.module.login.mapper.SysUserMapper;
import com.rgs.module.login.service.SysUserService;
import com.rgs.module.login.util.SecurityUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author Ren Gaoshuai
 * @ClassName SysUserServiceImpl
 * @date 2021-12-24 10:24
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public SysUserDO insert(SysUserDO sysUser) {
        if (Strings.isNullOrEmpty(sysUser.getId())){
            sysUser.setId(IdUtil.fastSimpleUUID());
        }
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setLastModifyTime(LocalDateTime.now());
        // 初始化密码
        passwordFormat(sysUser);
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    private void passwordFormat(SysUserDO sysUser) {
        String userPassword = sysUser.getPassword();
        if (!Strings.isNullOrEmpty(userPassword)) {
            userPassword = SecurityUtil.encryptPassword(userPassword);
            sysUser.setPassword(userPassword);
            //密码等级
            String passwordLevel = SecurityUtil.checkPasswordLevel(sysUser.getPassword());
            sysUser.setPasswordLevel(passwordLevel);
        }
    }

    @Override
    public void deleteByFilter(SysUserFilterDTO sysUserFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(SysUserDO.class, sysUserFilter);
        sysUserMapper.deleteByExample(example);
        return;
    }

    @Override
    public void updateByFilter(SysUserDO sysUser, SysUserFilterDTO sysUserFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(SysUserDO.class, sysUserFilter);
        sysUserMapper.updateByExample(sysUser, example);
    }

    @Override
    public void updateSelectiveByFilter(SysUserDO sysUser, SysUserFilterDTO sysUserFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(SysUserDO.class, sysUserFilter);
        sysUserMapper.updateByExampleSelective(sysUser, example);
    }

    @Override
    public List<SysUserDO> listByFilter(SysUserFilterDTO sysUserFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(SysUserDO.class, sysUserFilter);
        return sysUserMapper.selectByExample(example);
    }

    @Override
    public Optional<SysUserDO> selectByFilter(SysUserFilterDTO sysUserFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(SysUserDO.class, sysUserFilter);
        return sysUserMapper.selectByExample(example).stream().findFirst();
    }

    @Override
    public void update(SysUserDO sysUserDO) {
        sysUserDO.setLastModifyTime(LocalDateTime.now());
        sysUserMapper.updateByPrimaryKeySelective(sysUserDO);
    }
}
